﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Diagnostics;


namespace Mewdam.OAuth
{
    public class MewdamOAuth
    {
        public MewdamOAuthParameter Parameters=new MewdamOAuthParameter();
        const string TimelineHomeUrl = "timeline.aspx"; 

        public string  GetAuthorizeUrl(string consumerKey, string consumerSecret, string callback) 
        {

            OAuthBase ob=new OAuthBase(consumerKey,consumerSecret,callback);

            string queryString;
            queryString = ob.GetOAuthParameter(OAuthBase.OAuthMethod.RequestToken);
            WebRequestBuilder oauthRequest = new WebRequestBuilder(OAuthBase.HttpMethod.GET, OAuthBase.RequestTokenUrl, queryString);

            this.Parameters.response_body= oauthRequest.GetResponse();

            this.Parameters.oauth_token = Regex.Match(this.Parameters.response_body, @"oauth_token=([^&]+)").Groups[1].Value;
            this.Parameters.oauth_token_secret = Regex.Match(this.Parameters.response_body, @"oauth_token_secret=([^&]+)").Groups[1].Value;
            this.Parameters.oauth_callback_confirmed = Regex.Match(this.Parameters.response_body, @"oauth_callback_confirmed=([^&]+)").Groups[1].Value;

            Debug.Print("GetAuthorizeUrl\n" +
                        "consumer key: " + consumerKey + "\n" +
                        "consumer secret: " + consumerSecret);

            return OAuthBase.OAuthAuthorizeUrl + "?oauth_token=" + this.Parameters.oauth_token;
        }

        public void GetAccessToken(string consumerKey, string consumerSecret) 
        {
            OAuthBase ob = new OAuthBase(
                consumerKey, 
                consumerSecret, 
                this.Parameters.oauth_token, 
                this.Parameters.oauth_token_secret, 
                this.Parameters.oauth_verifier);

            string queryString;
            queryString = ob.GetOAuthParameter(OAuthBase.OAuthMethod.AccessToken);
            WebRequestBuilder oauthRequest = new WebRequestBuilder(OAuthBase.HttpMethod.GET, OAuthBase.AccessTokenUrl, queryString);

            this.Parameters.response_body = oauthRequest.GetResponse();

            this.Parameters.oauth_token = Regex.Match(this.Parameters.response_body, @"oauth_token=([^&]+)").Groups[1].Value;
            this.Parameters.oauth_token_secret = Regex.Match(this.Parameters.response_body, @"oauth_token_secret=([^&]+)").Groups[1].Value;
            this.Parameters.user_id = Regex.Match(this.Parameters.response_body, @"user_id=([^&]+)").Groups[1].Value;
            this.Parameters.screen_name = Regex.Match(this.Parameters.response_body, @"screen_name=([^&]+)").Groups[1].Value;

            Debug.Print("GetAccessToken\n" +
            "consumer key: " + this.Parameters.oauth_token + "\n" +
            "consumer secret: " + this.Parameters.oauth_token_secret+"\n"+
            "screen name: "+this.Parameters.screen_name);
        }



        public void GetHomeTimeline(string consumerKey, string consumerSecret,string page,string count)
        {

            OAuthBase ob = new OAuthBase(
                consumerKey,
                consumerSecret,
                this.Parameters.oauth_token,
                this.Parameters.oauth_token_secret);

            string queryString;
            ob.parameters.AddParameter(OAuthParameter.ParameterType.page, page);
            ob.parameters.AddParameter(OAuthParameter.ParameterType.count, count);
            queryString = ob.GetOAuthParameter(OAuthBase.OAuthMethod.HomeTimeline);
            WebRequestBuilder oauthRequest = new WebRequestBuilder(OAuthBase.HttpMethod.GET, OAuthBase.HomeTimelineUrl, queryString);

            Debug.Print("GetHomeTimeline\n" +
                        "consumer key: " + consumerKey + "\n" +
                        "consumer secret: " + consumerSecret);

            this.Parameters.response_body = oauthRequest.GetResponse();
        }

    }
}